package com.xh.petdogexpert.expert.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.expert.dto.Disease;
import com.xh.petdogexpert.expert.dto.DiseaseSymptomLink;
import com.xh.petdogexpert.expert.dto.Symptom;
import com.xh.petdogexpert.expert.mapper.DiseaseSymptomLinkMapper;
import com.xh.petdogexpert.expert.mapper.SymptomMapper;
import com.xh.petdogexpert.expert.service.ISymptomService;
import com.xh.petdogexpert.user.dto.User;

@Service
public class SymptomServiceImp implements ISymptomService {

	@Autowired
	SymptomMapper symptomMapper;

	@Autowired
	DiseaseSymptomLinkMapper diseaseSymptomLinkMapper;

	@Autowired
	HttpSession session;

	Logger logger = LoggerFactory.getLogger(SymptomServiceImp.class);

	/**
	 * 获取最多人选择的多个症状，按frequency排序
	 * 
	 * @author linxixin
	 * @date 2016年3月23日
	 */
	@Override
	public List<Symptom> querySymptoms(Symptom symptom, Integer page, Integer pageSize) {
		
		if (page != null && pageSize != null)
			PageHelper.startPage(page, pageSize);
		return symptomMapper.querySymptoms(symptom);
	}

	/**
	 * 根据前n个疾病返回所有疾病对应的所有症状，直接返回，expert
	 * 
	 * @author linxixin
	 * @date 2016年3月23日
	 * @see com.xh.petdogexpert.expert.service.ISymptomService#getSecondSymptoms(java.util.List)
	 */
	@Override
	public List<Symptom> getSecondSymptoms(List<Disease> diseases, List<String> symptomIds) {
		return symptomMapper.getSecondSymptoms(diseases, symptomIds);
	}

	/**
	 * 
	 * 每个一级分类下面获取多少个常用二级症状
	 * 
	 * @author linxixin
	 * @date 2016年3月23日 s * @see
	 *       com.xh.petdogexpert.expert.service.ISymptomService#
	 *       getLastTreeSymptoms(java.lang.Integer)
	 */
	@Override
	public List<Symptom> getLastTreeSymptoms(Integer count) {
		Symptom symptom = new Symptom();
		symptom.setParentId(0);// 获取一级症状
		List<Symptom> parents = querySymptoms(symptom, 0, 0);

		for (Symptom s : parents) {
			symptom.setParentId(s.getId());
			s.setChilds(querySymptoms(symptom, 0, 0));
		}

		return parents;
	}

	public void addSymptomFrequency(List<String> ids) {
		if (ids == null || ids.size() < 1)
			return;
		symptomMapper.addSymtomsFrency(ids);
	}

	@Override
	public List<Symptom> getSymptomsByKeyName(String keyName) {
		
		List<Symptom> symptoms = symptomMapper.getSymptomsByKeyName(keyName);
		return symptoms;
	}

	@Override
	public int addUpdateSymptom(Symptom symptom) throws BaseException {
		// TODO Auto-generated method stub

		try {
			if (symptom.getId() != null)
				return symptomMapper.updateByPrimaryKeySelective(symptom);
			else
				return symptomMapper.addSymptom(symptom);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("增加症状出错");
		}
	}

	@Override
	public Symptom getSymptom(Symptom symptom) throws BaseException {
		List<Symptom> symptoms = symptomMapper.querySymptoms(symptom);
		if (symptoms.size() > 1) {
			throw new BaseException("获取多于一个症状的数据");
		} else if (symptoms == null || symptoms.size() == 0) {
			throw new BaseException("不存在此症状的数据");
		} else {
			return symptoms.get(0);
		}

	}

	@Override
	public int deleteSymptom(Symptom symptom) throws BaseException {
		int count;
		try {
			count = symptomMapper.deleteSymptom(symptom);

		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseException("删除失败");
		}

		return count;
	}

	@Override
	public List<Symptom> queryLink(DiseaseSymptomLink link) {
		List<DiseaseSymptomLink> queryLinks = diseaseSymptomLinkMapper.queryLink(link);
		List<Symptom> symptoms = new ArrayList<>();

		for (DiseaseSymptomLink l : queryLinks) {
			int symptomId = l.getSymptomId();
			Symptom symptom = symptomMapper.selectByPrimaryKey(symptomId);
			symptom.setId(l.getId());
			symptom.setCf(l.getCf());
			symptoms.add(symptom);
		}
		return symptoms;
	}

	@Override
	public int addSymptomsLink(DiseaseSymptomLink link) {
		DiseaseSymptomLink searchLink = new DiseaseSymptomLink();
		searchLink.setDiseaseId(link.getDiseaseId());
		searchLink.setSymptomId(link.getSymptomId());

		List<DiseaseSymptomLink> queryLink = diseaseSymptomLinkMapper.queryLink(searchLink);
		if (queryLink.size() > 0) {
			double sum = 0;
			for (DiseaseSymptomLink l : queryLink) {
				sum += l.getCf();
			}
			sum = sum + link.getCf();
			searchLink.setCf(sum);
			System.out.println("!!!!!" + sum);
			diseaseSymptomLinkMapper.updateByPrimaryKeySelective(searchLink);
		} else
			diseaseSymptomLinkMapper.insert(link);
		return 0;
	}

	@Override
	public int deleteSymptomsLink(int linkId) {
		return diseaseSymptomLinkMapper.deleteByPrimaryKey(linkId);

	}

}
